package com.xunxi.mimi.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
import android.os.Looper;
import android.os.Process;
import android.view.KeyCharacterMap;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.util.Iterator;
import java.util.List;

/**
 * Created by lmy on 2018/3/28.
 * app
 */

public class AppUtils {

    private AppUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static boolean checkContext(Context context) {
        if(context == null) {
            return false;
        } else if(context instanceof Activity) {
            Activity activity = (Activity)context;
            return isActivityRunning(activity);
        } else {
            return true;
        }
    }

    public static boolean isMainThread() {
        return Looper.myLooper() == Looper.getMainLooper();
    }

    public static boolean isMainProcess(Context ctx) {
        String processName = getProcessName(ctx);
        return processName != null && !processName.contains(":");
    }

    public static boolean isInProcess(Context context, String pname) {
        String processName = getProcessName(context);
        return processName != null && processName.equals(pname);
    }

    public static String getProcessName(Context ctx) {
        int pid = Process.myPid();
        ActivityManager am = (ActivityManager)ctx.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if(runningApps == null) {
            return null;
        } else {
            Iterator var4 = runningApps.iterator();

            ActivityManager.RunningAppProcessInfo procInfo;
            do {
                if(!var4.hasNext()) {
                    return null;
                }

                procInfo = (ActivityManager.RunningAppProcessInfo)var4.next();
            } while(procInfo.pid != pid);

            return procInfo.processName;
        }
    }

    public static String getProcessName(Context cxt, int pid) {
        ActivityManager am = (ActivityManager)cxt.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if(runningApps == null) {
            return null;
        } else {
            Iterator var4 = runningApps.iterator();

            ActivityManager.RunningAppProcessInfo procInfo;
            do {
                if(!var4.hasNext()) {
                    return null;
                }

                procInfo = (ActivityManager.RunningAppProcessInfo)var4.next();
            } while(procInfo.pid != pid);

            return procInfo.processName;
        }
    }


    public static String getPackage(Context context) {
        String packageName = "";
        PackageInfo pi = getCurrentPackageInfo(context);
        if(null != pi) {
            packageName = pi.packageName;
        }

        return packageName;
    }

    public static boolean isPkgInstalled(Context context, String packageName) {
        try {
            PackageManager pm = context.getPackageManager();
            pm.getPackageInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException var3) {
            return false;
        }
    }

    public static PackageInfo getCurrentPackageInfo(Context context) {
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            return pi;
        } catch (Exception var3) {
            return null;
        }
    }

    public static void exit(Context context) {
        try {
            System.exit(0);
            Process.killProcess(Process.myPid());
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }

    public static void hideIme(Activity context, EditText editText) {
        if(context != null) {
            View v = context.getWindow().peekDecorView();
            if(v != null && v.getWindowToken() != null) {
                InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
            }

        }
    }


    @SuppressLint({"NewApi"})
    public static boolean checkDeviceHasNavigationBar(Context context) {
        boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(4);
        return !hasMenuKey && !hasBackKey;
    }

    public static int getNavigationBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        return resourceId != 0?resources.getDimensionPixelSize(resourceId):0;
    }

    public static void toggleNavigationBar(Activity activity, boolean isShow) {
        int uiOptions = activity.getWindow().getDecorView().getSystemUiVisibility();
        int newUiOptions = uiOptions;
        boolean isImmersiveModeEnabled = (uiOptions | 4096) == uiOptions;
        if(!isImmersiveModeEnabled || isShow) {
            if(isImmersiveModeEnabled || !isShow) {
                if(Build.VERSION.SDK_INT >= 14) {
                    newUiOptions = uiOptions ^ 2;
                }

                if(Build.VERSION.SDK_INT >= 16) {
                    newUiOptions ^= 4;
                }

                if(Build.VERSION.SDK_INT >= 18) {
                    newUiOptions ^= 4096;
                }

                activity.getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
            }
        }
    }

    public static String getCurProcessName(Context context) {
        int pid = Process.myPid();
        ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        Iterator var3 = mActivityManager.getRunningAppProcesses().iterator();

        ActivityManager.RunningAppProcessInfo appProcess;
        do {
            if(!var3.hasNext()) {
                return null;
            }

            appProcess = (ActivityManager.RunningAppProcessInfo)var3.next();
        } while(appProcess.pid != pid);

        return appProcess.processName;
    }

    public static boolean isWeChatAvilible(Context context) {
        PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if(pinfo != null) {
            for(int i = 0; i < pinfo.size(); ++i) {
                String pn = ((PackageInfo)pinfo.get(i)).packageName;
                if(pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }

        return false;
    }


    /**
     * 显示输入框
     * @param editText
     */
    public static void showIme(EditText editText) {
        editText.requestFocus();
        InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context
                .INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    }

    /**
     * 隐藏输入框
     * @param activity
     */
    public static void hideIme(Activity activity) {
        if (activity != null) {
            View currentFocus = activity.getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                if(imm != null) {
                    imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        }
    }

    /**
     * 隐藏输入框
     * @param editText
     */
    public static void hideIme(EditText editText) {
        InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context
                .INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        editText.clearFocus();
    }

    /**
     * 获取app name
     * @param context
     * @return
     */
    public static String getAppName(Context context){
        String appName = "";
        try {
            PackageManager pm = context.getPackageManager();
            appName = context.getApplicationInfo().loadLabel(pm).toString();
        }catch (Exception e){
        }
        return appName;
    }

    public static boolean isActivityRunning(Activity activity) {
        try {
            return activity != null && !activity.isFinishing() && (Build.VERSION.SDK_INT < 17 || !activity.isDestroyed());
        } catch (Exception var2) {
            var2.printStackTrace();
            return false;
        }
    }
}
